<template>
	<el-card class="form-container">
		<el-form :model="dataForm" ref="dataForm" :rules="dataRule" @keyup.enter.native="dataFormSubmitHandle()" label-width="90px">
			<el-form-item label="专区名称" prop="prefectureName">
				<el-input v-model="dataForm.prefectureName" placeholder="请输入专区名称"></el-input>
			</el-form-item>
			<el-form-item label="副标题" prop="prefectureSketch">
				<el-input v-model="dataForm.prefectureSketch" placeholder="请输入专区副标题"></el-input>
			</el-form-item>
			<el-form-item label="封面图" prop="prefectureLogo">
				<el-upload
					class="avatar-uploader"
					action="#"
					:show-file-list="false"
					:on-success="handleAvatarPrefectureLogoSuccess"
					:before-upload="beforeAvatarUpload"
					:auto-upload="true"
					:data="extraParameters"
					:http-request="uploadImagePrefectureLogo">
					<img v-if="dataForm.prefectureLogo" :src="dataForm.prefectureLogo" class="avatar">
					<i v-else class="el-icon-plus avatar-uploader-icon"></i>
				</el-upload>
			</el-form-item>
			<el-form-item label="背景图" prop="prefectureBackdrop">
				<el-upload
					class="avatar-uploader"
					action="#"
					:show-file-list="false"
					:on-success="handleAvatarPrefectureBackdropSuccess"
					:before-upload="beforeAvatarUpload"
					:auto-upload="true"
					:data="extraParameters"
					:http-request="uploadImagePrefectureBackdrop">
					<img v-if="dataForm.prefectureBackdrop" :src="dataForm.prefectureBackdrop" class="avatar">
					<i v-else class="el-icon-plus avatar-uploader-icon"></i>
				</el-upload>
			</el-form-item>
			<el-form-item label="排序" prop="prefectureSort">
				<el-input v-model="dataForm.prefectureSort" placeholder="请输入排序"></el-input>
			</el-form-item>
			<el-form-item label="是否显示" prop="isShow">
				<el-radio-group v-model="dataForm.isShow">
					<el-radio label="1">显示</el-radio>
					<el-radio label="2">隐藏</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item>
				<el-button @click="cancel">取消</el-button>
				<el-button type="primary" @click="submit('dataForm')">确定</el-button>
			</el-form-item>
		</el-form>
	</el-card>
</template>

<script>
	import { addPrefecture } from '@/api/activity/prefecture'
	import { uploadImage } from '@/api/common/upload'
	export default {
		name: 'add-prefecture',
		data() {
			return {
				dataForm: {
					prefectureName: '',
					prefectureSketch: '',
					prefectureLogo: '',
					prefectureBackdrop: '',
					prefectureSort: '',
					isShow: '',
				},
				dataRule: {
					prefectureName: [{
							required: true,
							message: '请输入专区主体名称',
							trigger: 'blur'
						},
					],
					prefectureSketch: [{
							required: true,
							message: '请输入专区副标题',
							trigger: 'blur'
						},
					],
					prefectureLogo: [{
							required: true,
							message: '请输入专区封面图',
							trigger: 'blur'
						},
					],
					prefectureBackdrop: [{
							required: true,
							message: '请输入专区背景图',
							trigger: 'blur'
						},
					],
					goodsNumber: [{
							required: true,
							message: '请输入单品数量',
							trigger: 'blur'
						},
					],
					browsingVolume: [{
							required: true,
							message: '请输入专区浏览量',
							trigger: 'blur'
						},
					],
					prefectureSort: [{
							required: true,
							message: '请输入排序',
							trigger: 'blur'
						},
						
					],
					isShow: [{
							required: true,
							message: '请输入是否显示  1.显示 2.不显示',
							trigger: 'blur'
						},
					]
				},
				extraParameters:{
					uploadType:'eyeglass-user'
				},
				
			}
		},
		methods: {
			cancel() {
				this.$router.go(-1)
			},
			submit() {
				const set = this.$refs;
				set['dataForm'].validate((valid) => {
					if (!valid) {
						return false
					}
					addPrefecture(this.dataForm).then(() => {
						this.$notify({
							title: '添加成功',
							message: '添加成功',
							type: 'success',
							duration: 2000,
							onClose: this.$router.go(-1)
						})
					})
				})
			},
			beforeAvatarUpload(file) {
				const isJPG = file.type === 'image/jpeg';
				const isLt2M = file.size / 1024 / 1024 < 2;
				if (!isJPG) {
					this.$message.error('上传头像图片只能是 JPG 格式!');
				}
				if (!isLt2M) {
					this.$message.error('上传头像图片大小不能超过 2MB!');
				}
				return isJPG && isLt2M;
			},
			handleAvatarPrefectureLogoSuccess(res, file) {
				this.dataForm.prefectureLogo = URL.createObjectURL(file.raw);
			},
			uploadImagePrefectureLogo(val){
				let formData = new FormData();
				formData.append('uploadType',this.extraParameters.uploadType)
				formData.append('files',val.file)
				uploadImage(formData).then(res =>{
					this.dataForm.prefectureLogo = res.data.responseData[0]
				})
			},
			handleAvatarPrefectureBackdropSuccess(res, file) {
				this.dataForm.prefectureBackdrop = URL.createObjectURL(file.raw);
			},
			uploadImagePrefectureBackdrop(val){
				let formData = new FormData();
				formData.append('uploadType',this.extraParameters.uploadType)
				formData.append('files',val.file)
				uploadImage(formData).then(res =>{
					this.dataForm.prefectureBackdrop = res.data.responseData[0]
				})
			},
		}
	}
</script>

<style>
.avatar-uploader .el-upload {
	border: 1px dashed #d9d9d9;
	border-radius: 6px;
	cursor: pointer;
	position: relative;
	overflow: hidden;
}
.avatar-uploader .el-upload:hover {
	border-color: #409EFF;
}
.avatar-uploader-icon {
	font-size: 28px;
	color: #8c939d;
	width: 80px;
	height: 80px;
	line-height: 80px;
	text-align: center;
}
.avatar {
	width: 80px;
	height: 80px;
	display: block;
}
</style>
